/*
@title: Quiz Game
@author: Nandi
@description: A simple quiz game with 10 questions.
@tags: []
@addedOn: 2025-08-09
*/


const questionBoxLeft = "q"
const questionBoxRight = "b"
const questionBoxMid = "m"
const wIcon = "w"
const aIcon = "a"
const sIcon = "s"
const dIcon = "d"
const goodAns = tune`
500: B4~500,
15500`
const badAns = tune`
500: F4~500,
15500`

const finishTune = tune`
410.958904109589: B4~410.958904109589,
410.958904109589: C5~410.958904109589,
410.958904109589: C5~410.958904109589,
410.958904109589: B4~410.958904109589,
410.958904109589,
410.958904109589: D5~410.958904109589,
410.958904109589: B4~410.958904109589,
821.917808219178,
410.958904109589: D5~410.958904109589 + B4~410.958904109589,
821.917808219178,
410.958904109589: D5~410.958904109589 + B4~410.958904109589,
821.917808219178,
410.958904109589: E5~410.958904109589,
410.958904109589,
410.958904109589: C5~410.958904109589,
821.917808219178,
410.958904109589: E5~410.958904109589 + C5~410.958904109589,
1232.876712328767,
410.958904109589: F5~410.958904109589,
410.958904109589,
410.958904109589: C5~410.958904109589,
410.958904109589,
410.958904109589: F5~410.958904109589 + C5~410.958904109589,
1232.876712328767`

let answerInput;
let correctAnswer;
let score = 0;

setLegend(
  [questionBoxLeft, bitmap`
0000000000000000
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0...............
0000000000000000`],
  [questionBoxMid, bitmap`
0000000000000000
................
................
................
................
................
................
................
................
................
................
................
................
................
................
0000000000000000`],
  [questionBoxRight, bitmap`
0000000000000000
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
...............0
0000000000000000`],
  [dIcon, bitmap`
................
................
................
......000.......
......0..0......
......0...0.....
......0...0.....
......0...0.....
......0...0.....
......0...0.....
......0..0......
......000.......
................
................
................
................`],
  [aIcon, bitmap`
................
................
................
................
................
.......00.......
......0..0......
......0..0......
......0000......
......0..0......
......0..0......
......0..0......
................
................
................
................`],
  [sIcon, bitmap`
................
................
................
................
.....00000......
.....0..........
.....0..........
.....00000......
.........0......
.........0......
.....00000......
................
................
................
................
................`],
  [wIcon, bitmap`
................
................
................
................
................
....0.....0.....
....0.....0.....
....0.....0.....
....0..0..0.....
....0.0.0.0.....
....00...00.....
................
................
................
................
................`]

)



setSolids([])

let level = 0
const levels = [
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
qmbwsqmb
qmbadqmb`,
  map`
........
........
........
........
........`
]



setMap(levels[level])
if (level == 0) {
  addText("Score: " + score, { x: 1, y: 2, color: color`0` })
  correctAnswer = "a";
  addText("What is the\ncapital of \nFrance?", { x: 3, y: 4, color: color`0` })
  addText("Madrid", { x: 1, y: 10, color: color`0` }) //w
  addText("Rome", { x: 14, y: 10, color: color`0` }) //s
  addText("Berlin", { x: 14, y: 13, color: color`0` }) //d
  addText("Paris", { x: 1, y: 13, color: color`0` }) //a
}


onInput("w", () => {
  answerInput = "w";
})
onInput("a", () => {
  answerInput = "a";
})
onInput("s", () => {
  answerInput = "s";
})
onInput("d", () => {
  answerInput = "d";
})
let isCorrectAnswer = false;

afterInput(() => {

  if (answerInput == correctAnswer) {
    playTune(goodAns);
    score++;
    level++;
    clearText();
    addText("Score: " + score, { x: 1, y: 2, color: color`0` })

    iscorrectAnswer = true;

    addText("Correct answer!\n Next in 2s...", {
      x: 3,
      y: 4,
      color: color`4`
    })
  } else {
    playTune(badAns);
    isCorrectAnswer = false;
    clearText();
    addText("Incorrect answer!\n Next in 2s...", {
      x: 3,
      y: 4,
      color: color`3`
    })
  }

  setTimeout(() => {
    clearText();
    // Show the question for the current level

    setMap(levels[level]);

    if (level == 1) {
      correctAnswer = "d";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("What is the\nchemical symbol\nfor water?", { x: 3, y: 4, color: color`0` })
      addText("O2", { x: 1, y: 10, color: color`0` })
      addText("CO2", { x: 14, y: 10, color: color`0` })
      addText("H2O", { x: 14, y: 13, color: color`0` })
      addText("HO", { x: 1, y: 13, color: color`0` })
    }
    if (level == 2) {
      correctAnswer = "a";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("What is the\nsmallest\nprime number?", { x: 3, y: 4, color: color`0` })
      addText("1", { x: 1, y: 10, color: color`0` })
      addText("0", { x: 14, y: 10, color: color`0` })
      addText("4", { x: 14, y: 13, color: color`0` })
      addText("2", { x: 1, y: 13, color: color`0` })
    }
    if (level == 3) {
      correctAnswer = "d";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("How many\n continents\nare there on\nEarth?", { x: 3, y: 4, color: color`0` })
      addText("5", { x: 1, y: 10, color: color`0` })
      addText("6", { x: 14, y: 10, color: color`0` })
      addText("7", { x: 14, y: 13, color: color`0` })
      addText("8", { x: 1, y: 13, color: color`0` })
    }
    if (level == 4) {
      correctAnswer = "a";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("What is the main\n currency\nused in the\n US?", { x: 3, y: 4, color: color`0` })
      addText("Euro", { x: 1, y: 10, color: color`0` })
      addText("Pound", { x: 14, y: 10, color: color`0` })
      addText("Yen", { x: 14, y: 13, color: color`0` })
      addText("Dollar", { x: 1, y: 13, color: color`0` })
    }
    if (level == 5) {
      correctAnswer = "s";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("Which metal is\nliquid at room\n temperature?", { x: 3, y: 4, color: color`0` })
      addText("Iron", { x: 1, y: 10, color: color`0` })
      addText("Mercury", { x: 13, y: 10, color: color`0` })
      addText("Copper", { x: 14, y: 13, color: color`0` })
      addText("Gold", { x: 0, y: 13, color: color`0` })
    }
    if (level == 6) {
      correctAnswer = "d";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("What is the\nfastest\nland animal?", { x: 3, y: 4, color: color`0` })
      addText("Lion", { x: 1, y: 10, color: color`0` })
      addText("Horse", { x: 14, y: 10, color: color`0` })
      addText("Cheetah", { x: 13, y: 13, color: color`0` })
      addText("Tiger", { x: 0, y: 13, color: color`0` })
    }
    if (level == 7) {
      correctAnswer = "a";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("What is the \nlargest planet in\n our\nsolar system?", { x: 3, y: 4, color: color`0` })
      addText("Earth", { x: 1, y: 10, color: color`0` })
      addText("Mars", { x: 14, y: 10, color: color`0` })
      addText("Saturn", { x: 14, y: 13, color: color`0` })
      addText("Jupiter", { x: 0, y: 13, color: color`0` })
    }
    if (level == 8) {
      correctAnswer = "a";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("How many sides\ndoes a \nhexagon have?", { x: 3, y: 4, color: color`0` })
      addText("5", { x: 1, y: 10, color: color`0` })
      addText("8", { x: 14, y: 10, color: color`0` })
      addText("4", { x: 14, y: 13, color: color`0` })
      addText("6", { x: 1, y: 13, color: color`0` })
    }
    if (level == 9) {
      correctAnswer = "s";
      addText("Score: " + score, { x: 1, y: 2, color: color`0` })
      addText("Which country is\n famous for the\n Great Wall?", { x: 3, y: 4, color: color`0` })
      addText("Japan", { x: 1, y: 10, color: color`0` })
      addText("China", { x: 14, y: 10, color: color`0` })
      addText("India", { x: 14, y: 13, color: color`0` })
      addText("Albania", { x: 1, y: 13, color: color`0` })
    }
    if (level == 10) {
      isGameOver = true;
      playTune(finishTune);
      addText("Final\nscore: " + score, { x: 6, y: 6, color: color`0` });
      addText("Thanks for\n playing! ", { x: 4, y: 4, color: color`0` });
      addText("Reset to\n play again! ", { x: 4, y: 8, color: color`0` });
    }
  
  }, 2000); 
})
